<script setup lang="ts">
import AddWrite from './components/addWrite.vue'
import { ContentDetailWrap } from '@/components/ContentDetailWrap'
import { ref, unref } from 'vue'
import { ElButton } from 'element-plus'
import { useI18n } from '@/hooks/web/useI18n'
import { useRouter } from 'vue-router'
import { userAdd } from '@/api/common'
import { userType } from '@/api/common/types'
import { useEmitt } from '@/hooks/web/useEmitt'

const { emitter } = useEmitt()

const { push } = useRouter()

const { t } = useI18n()

const addWrite = ref<ComponentRef<typeof AddWrite>>()

const loading = ref(false)

const save = async () => {
  const write = unref(addWrite)
  console.log('---addWrite---', write?.getFormData())

  const data = {
    id: write?.getFormData()?.id,
    username: write?.getFormData()?.username,
    nickname: write?.getFormData()?.nickname,
    email: write?.getFormData()?.email,
    mobile: write?.getFormData()?.mobile,
    // email: write?.getFormData()?.email,
    gender: write?.getFormData()?.gender,
    birthday: write?.getFormData()?.birthday,
    bio: write?.getFormData()?.bio,
    money: write?.getFormData()?.money,
    score: write?.getFormData()?.score,
    loginfailure: write?.getFormData()?.loginfailure,
    joinip: write?.getFormData()?.joinip,
    jointime: write?.getFormData()?.jointime,
    status: write?.getFormData()?.status
  } as userType
  // data.buytime = '' + parseInt(data.buytime) / 1000
  console.log('---客户新增参数---', data)
  const res = await userAdd(data)
  if (res) {
    emitter.emit('getList', 'add')
    push('/personnelManagement/loginUser')
  }
}
</script>

<template>
  <ContentDetailWrap :title="t('exampleDemo.add')" @back="push('/personnelManagement/loginUser')">
    <!-- <Write ref="writeRef" /> -->
    <AddWrite ref="addWrite" />

    <template #right>
      <ElButton type="primary" :loading="loading" @click="save">
        {{ t('exampleDemo.save') }}
      </ElButton>
    </template>
  </ContentDetailWrap>
</template>
